package com.blank.util;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.net.ssl.SSLContext;

import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

	private CloseableHttpClient httpclient = null;

	public String SSLPost(String url, Map<String, String> paramers, String charset, List<Cookie> cookies) {
		BasicCookieStore cookieStore = new BasicCookieStore();
		CloseableHttpClient httpclient = createSSLClientDefault();
		try {
			HttpPost httpPost = new HttpPost(url);
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			Iterator iterator = paramers.entrySet().iterator();
			while (iterator.hasNext()) {
				Entry<String, String> elem = (Entry<String, String>) iterator.next();
				list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
			}
			if (list.size() > 0) {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
				httpPost.setEntity(entity);
			}
			CloseableHttpResponse response = httpclient.execute(httpPost);
			try {
				return EntityUtils.toString(response.getEntity());
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		return null;

	}

	public String SSLGet(String url, String charset, List<Cookie> cookies) {
		BasicCookieStore cookieStore = new BasicCookieStore();
		CloseableHttpClient httpclient = createSSLClientDefault();
		try {
			HttpGet httpGet = new HttpGet(url);
	        httpGet.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
	        httpGet.addHeader("Host","jiangmen.11467.com");
	        httpGet.addHeader("Cookie","ASPSESSIONIDCCRAQCBD=MMHCINIBIPGNLCHIBBDAKEGD; ASPSESSIONIDCATARCBD=CKHLFAFBAHIGEFKMLFKDBKDG; Hm_lvt_819e30d55b0d1cf6f2c4563aa3c36208=1464317020,1464588688; Hm_lpvt_819e30d55b0d1cf6f2c4563aa3c36208=1464589106; Vcode=%D1%C0%D4%C6%D4%C2");
	        httpGet.addHeader("Accept-Encoding","gzip, deflate, sdch");
	        httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.8");
	        httpGet.addHeader("Cache-Control","max-age=0");
	        httpGet.addHeader("Connection","keep-alive");
	        
			CloseableHttpResponse response = httpclient.execute(httpGet);
			try {
				return EntityUtils.toString(response.getEntity());
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
		return null;

	}

	/**
	 * 创建SSL客户端
	 * 
	 * @return
	 */
	public CloseableHttpClient createSSLClientDefault() {

		if (httpclient != null)
			return httpclient;

		try {
			SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
				// 信任所有
				public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
					return true;
				}
			}).build();
			System.out.println("构建SSLClinet中");
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
			System.out.println("构建SSLClinet完成");
			httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
			return httpclient;
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}
		
		httpclient = HttpClients.createDefault();
		return httpclient;
	}

}
